package com.acmen.easyapi.executor;

import com.acmen.easyapi.model.Api;
import com.acmen.easyapi.model.App;
import com.acmen.easyapi.model.Environment;
import com.acmen.easyapi.model.MenuTree;
import com.acmen.easyapi.repository.ApiRepository;
import com.acmen.easyapi.repository.AppRepository;
import com.acmen.easyapi.repository.EnvironmentRepository;
import com.acmen.easyapi.util.ApiMenuTreeUtil;
import com.acmen.easyapi.util.StringUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 应用查询执行器
 * @author acmen
 */
@Component
public class AppQueryExe {
    @Autowired
    private AppRepository appRepository;
    @Autowired
    private ApiRepository apiRepository;
    @Autowired
    private EnvironmentRepository environmentRepository;


    public String note(HttpServletRequest request, ModelMap model) {
        model.put("contextPath", request.getContextPath());
        String appName = request.getParameter("appName");
        model.put("app", appRepository.selectByName(appName));
        List<Environment> environments = environmentRepository.getGatewayEnvironments(appName);
        model.put("environments", environments);
        return "/app/note";
    }

    public String appDetail(HttpServletRequest request,String appName,ModelMap model) {
        model.put("contextPath", request.getContextPath());
        String apiId = request.getParameter("apiId");
        App app = null;
        if(StringUtil.isNotEmpty(apiId)){
            Api api = apiRepository.selectById(apiId);
            if(api!=null){
                app = appRepository.selectByName(api.getAppName());
                appName = app.getName();
            }else{
                return "/app/404";
            }
        }else{
            app = appRepository.selectByName(appName);
        }
        List<Api> apis = apiRepository.selectListByAppName(app.getName());
        app.setApis(apis);
        model.put("app", app);
        model.put("apiId", StringUtil.isEmpty(apiId)?"":apiId);
        MenuTree root = new MenuTree("根");
        root.setApiId("root");
        MenuTree devGuide = new MenuTree("开发指南");
        devGuide.getChildrens().add(new MenuTree("● 接口公共说明","/app/note?appName="+appName));
        root.getChildrens().add(devGuide);
        root.getChildrens().addAll(ApiMenuTreeUtil.getApiMenuTree(app.getApis()).getChildrens());
        MenuTree apiManager = new MenuTree("接口管理");
        apiManager.getChildrens().add(new MenuTree("● 接口导出/删除","/app/api-tree?appName="+appName));
        apiManager.getChildrens().add(new MenuTree("● 接口并发测试","/test/pressure-test?appName="+appName));
        root.getChildrens().add(apiManager);

        model.put("app", app);
        model.put("menuTree", JSON.toJSONString(root));
        return "/app/index";
    }

    public MenuTree getApiMenuTree(String appName){
        App app = appRepository.selectByName(appName);
        List<Api> apis = apiRepository.selectListByAppName(app.getName());
        app.setApis(apis);
        MenuTree root = ApiMenuTreeUtil.getApiMenuTree(app.getApis());
        return root;
    }
}
